package com.iap.admin.mediator
{
	import com.iap.client.event.FillerEvent;
	import com.iap.common.event.SampleEvent;
	import com.iap.common.event.SampleFillerEvent;
	import com.iap.admin.event.VectorEvent;
	import com.iap.common.signal.FillerRequestSignal;
	import com.iap.common.signal.SampleRequestSignal;
	import com.iap.admin.signal.VectorRequestSignal;
	import com.iap.admin.view.SampleView;
	import com.iap.admin.view.sample.SampleEditView;
	import com.iap.admin.view.sample.SampleFillerSelectorWindow;
	import com.iap.admin.view.sample.code.SampleEditViewCode;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.app.model.ServiceResponse;
	import com.iap.common.model.Filler;
	import com.iap.common.model.Sample;
	import com.iap.common.model.VectorModel;
	
	import flash.events.Event;
	import flash.text.ReturnKeyLabel;
	
	import mx.collections.ArrayCollection;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.Group;
	import com.iap.common.mediator.BaseMediator;
	
	public class SampleEditMediator extends BaseMediator
	{
		[Inject]
		public var view:SampleEditView;
		[Inject]
		public var request:SampleRequestSignal;
		[Inject]
		public var vectorRequest:VectorRequestSignal;
		[Inject]
		public var fillerRequest:FillerRequestSignal;

		override public function onRegister():void
		{
			view.onCancel.add(onCancel);
			view.onSubmit.add(onSubmit);
			addContextListener(SampleEvent.RESPONSE, responseHandler);
			addContextListener(VectorEvent.RESPONSE, vectorResponseHandler);
			addContextListener(FillerEvent.RESPONSE, fillerResponseHandler);
			dispatchInitRequest();
		}

		private function dispatchInitRequest():void
		{
			AppGlobal.startWait();
			vectorRequest.dispatch(new VectorModel(), VectorRequestSignal.GET);
		}
		
		private function dispatchFillerRequest():void
		{
			fillerRequest.dispatch(new Filler(), FillerRequestSignal.GET);
		}

		private function onCancel(itemName:String):void
		{
			switch(itemName)
			{
				case SampleEditViewCode.CANCEL_NAME:
				{
					optionBack();
					break;
				}
			}
		}

		private function optionBack():void
		{
			setView(view.parent as Group, new SampleView(), FX_GLIDE_OUT);
		}

		private function onSubmit(model:Sample, task:String):void
		{
			AppGlobal.startWait();
			request.dispatch(model, task);
		}

		private function responseHandler(evt:SampleEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.status.code != StatusCode.OK) return;
			optionBack();
		}

		private function vectorResponseHandler(evt:VectorEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.status.code != StatusCode.OK) return;
			var response:ServiceResponse = evt.response;
			setVectorList(new ArrayCollection(response.result as Array));
			dispatchFillerRequest();
		}
		
		private function setVectorList(items:ArrayCollection):void
		{
			view.vectorModelForm.dataProvider = items;
			if(view.model == null) return;
			var sample:Sample = view.model as Sample;
			if(sample.vectorModel == null) return;
			for(var i:Number = 0; i<items.length; i++)
			{
				if((items[i] as VectorModel).id == sample.vectorModel.id)
				{
					view.vectorModelForm.selectedIndex = i;
					view.updatePreviewFromModel();
				}
			}
		}

		private function fillerResponseHandler(evt:FillerEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.status.code != StatusCode.OK) return;
			var response:ServiceResponse = evt.response;
			view.fillers = new ArrayCollection(response.result as Array);
		}
	}
}